<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;
use app\common\model\applymanager\Apply;
use app\common\model\applymanager\Business;
use think\Db;
use think\Config;
use fast\Random;

/**
 * 申请加盟接口
 */
class Joinapply extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => '123']);
    }

    /*
     * 申请加盟
     */
    public function addApply(){
        $user_id = $this->auth->getUserinfo()['id'];
        
        $where['user_id'] = $user_id;
        $allow_apply = Apply::where($where)->field('`status`')->find();
        if($allow_apply){
            if($allow_apply['status'] == 0){
                $this->error('申请等待审核，请勿重复提交');
            }

            if($allow_apply['status'] == 1){
                $this->error('申请已审核通过，请勿重复提交');
            }

            if($allow_apply['status'] == 2){
                $allow_apply->delete();
            }
        }

        $data = $this->request->post();
        $data['user_id'] = $user_id;
        $data['code'] = Random::alnum(10);
        unset($data['token']);
        if(isset($data['share_from'])) unset($data['share_from']);
        $is_success = Apply::create($data);
        $this->success('提交成功', $is_success);
    }

    /*
     * 加盟信息
     */
    public function getApplyInfo(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $where['status'] = array('in', '0, 1');
        $field = 'id, code, real_name, gender, province_id, city_id, county_id, age, qq, mobile,
            createtime, user_id,status';
        $huter_info = Apply::where($where)->field($field)->find();
        $this->success('加盟信息', $huter_info);
    }

    /*
     * 添加我的业务
     */
    public function addBussiness(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $field = 'id, real_name, code, `status`';
        $apply_info = Apply::where($where)->field($field)->order('id DESC')->find();
        if($apply_info){
            if($apply_info['status'] == 0){
                $this->error('加盟申请等待审核中');
            }
            if($apply_info['status'] == 2){
                $this->error('加盟申请未通过审核，请联系平台后重新申请');
            }
        }else{
            $this->error('请先提交加盟申请');
        }
        $data = $this->request->post();
        $data['user_id'] = $user_id;
        $data['join_apply_id'] = $data['join_apply_id'] ? $data['join_apply_id'] : $apply_info['id'];
        $data['join_no'] = $data['join_no'] ? $data['join_no'] : $apply_info['code'];
        $data['join_name'] = $data['join_name'] ? $data['join_name'] : $apply_info['real_name'];
        //$data['prod_no'] = $data['prod_no'] ? $data['prod_no'] : Random::alnum(15);
        //$data['prod_name'] = $data['prod_name'] ? $data['prod_name'] : Random::alnum(15);
        
        $data['business_time'] = strtotime($data['business_time']);
        unset($data['token']);

        $is_success = Business::create($data);
        $this->success('提交成功', $is_success);
    }
    /*
     * 我的业务
     */
    public function getBussiness(){
        $user_id = $this->auth->getUserinfo()['id'];
        $where['user_id'] = $user_id;
        $order = 'id DESC';
        $productionList = Business::where($where)->order($order)->paginate();
        $this->success('我的业务', $productionList);
    }

    
}
